/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.xtd.view;

import com.xtd.facade.admin.SystemPropertyFacade;
import com.xtd.facade.employment.DepartmentPostFacade;
import com.xtd.facade.employment.EmployeeFacade;
import com.xtd.model.employment.DepartmentPost;
import com.xtd.model.employment.Employee;
import com.xtd.util.CommodityPropertyEnum;
import com.xtd.util.FormTypeEnum;
import com.xtd.util.PostingTypeEnum;
import com.xtd.util.PayrollCalculatorEnum;
import com.xtd.util.SystemPropertyEnum;
import java.util.ArrayList;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ApplicationScoped;
import javax.faces.model.SelectItem;

/**
 *
 * @author Lu Yuanbin
 */
@ManagedBean
@ApplicationScoped
public class ApplicationBean extends AbstractManager {

    @EJB
    SystemPropertyFacade systemPropertyFacade;
    @EJB
    EmployeeFacade employeeFacade;
    @EJB
    DepartmentPostFacade departmentPostFacade;
    private List<SelectItem> sourceItems;
    private List<SelectItem> inclinationItems;
    private List<SelectItem> emphasisItems;
    private List<SelectItem> commdoityPropertyItems;
    private List<SelectItem> commdoityPropertyItemsWithoutPackage;
    private EnumMap<CommodityPropertyEnum, List<SelectItem>> commodityTypeItemMap;
    private List<SelectItem> unitItems;
    private List<SelectItem> employeeSelectItems;
    private List<SelectItem> departmentPostSelectItems;
    private Map<Integer, List<SelectItem>> employeeSelectItemMap;
    private List<SelectItem> formTypeSelectList;
    private List<SelectItem> paymentMethodSelectItems;
    private List<SelectItem> postingTypeSelectItems;
    private List<SelectItem> payrollCalculatorSelectItems;

    /** Creates a new instance of ApplicationBean */
    public ApplicationBean() {
    }

    @PostConstruct
    public void initSelectItem() {
        sourceItems = getCompositeSystemProperty(SystemPropertyEnum.客户来源.toString());
        inclinationItems = getCompositeSystemProperty(SystemPropertyEnum.来访意向.toString());
        emphasisItems = getCompositeSystemProperty(SystemPropertyEnum.侧重点.toString());
        paymentMethodSelectItems = getCompositeSystemProperty(SystemPropertyEnum.付款方式.toString());
        setCommodityTypeItemMap(new EnumMap<CommodityPropertyEnum, List<SelectItem>>(CommodityPropertyEnum.class));
        for (CommodityPropertyEnum cce : CommodityPropertyEnum.values()) {
            getCommodityTypeItemMap().put(cce, getCompositeSystemProperty(cce.toString()));

        }
        unitItems = getCompositeSystemProperty(SystemPropertyEnum.单位.toString());


    }

    public List<SelectItem> getCompositeSystemProperty(String name) {

        String value = systemPropertyFacade.getSystemPropertyValue(name);
        String[] items = value.split(";");
        List<SelectItem> itemList = new ArrayList();
        itemList.add(new SelectItem(null, "请选择"));
        for (String item : items) {
            itemList.add(new SelectItem(item, item));
        }
        return itemList;
    }

    /**
     * @return the sourceItems
     */
    public List<SelectItem> getSourceItems() {
        return sourceItems;
    }

    /**
     * @param sourceItems the sourceItems to set
     */
    public void setSourceItems(List<SelectItem> sourceItems) {
        this.sourceItems = sourceItems;
    }

    /**
     * @return the inclinationItems
     */
    public List<SelectItem> getInclinationItems() {
        return inclinationItems;
    }

    /**
     * @param inclinationItems the inclinationItems to set
     */
    public void setInclinationItems(List<SelectItem> inclinationItems) {
        this.inclinationItems = inclinationItems;
    }

    /**
     * @return the emphasisItems
     */
    public List<SelectItem> getEmphasisItems() {
        return emphasisItems;
    }

    /**
     * @param emphasisItems the emphasisItems to set
     */
    public void setEmphasisItems(List<SelectItem> emphasisItems) {
        this.emphasisItems = emphasisItems;
    }

    /**
     * @return the commdoityCategoryItems
     */
    public List<SelectItem> getCommdoityPropertyItems() {
        if (commdoityPropertyItems == null) {
            commdoityPropertyItems = new ArrayList();
            commdoityPropertyItems.add(new SelectItem(null, "请选择"));
            for (CommodityPropertyEnum cce : CommodityPropertyEnum.values()) {
                commdoityPropertyItems.add(new SelectItem(cce, cce.toString()));
            }
        }
        return commdoityPropertyItems;
    }

    /**
     * @param commdoityCategoryItems the commdoityCategoryItems to set
     */
    public void setCommdoityPropertyItems(List<SelectItem> commdoityPropertyItems) {
        this.commdoityPropertyItems = commdoityPropertyItems;
    }

    /**
     * @return the commodityTypeItemMap
     */
    public Map<CommodityPropertyEnum, List<SelectItem>> getCommodityTypeItemMap() {

        return commodityTypeItemMap;
    }

    /**
     * @param commodityTypeItemMap the commodityTypeItemMap to set
     */
    public void setCommodityTypeItemMap(EnumMap<CommodityPropertyEnum, List<SelectItem>> commodityTypeItemMap) {
        this.commodityTypeItemMap = commodityTypeItemMap;
    }

    /**
     * @return the unitItems
     */
    public List<SelectItem> getUnitItems() {
        return unitItems;
    }

    /**
     * @param unitItems the unitItems to set
     */
    public void setUnitItems(List<SelectItem> unitItems) {
        this.unitItems = unitItems;
    }

    /**
     * @return the commdoityPropertyItemsWithoutPackage
     */
    public List<SelectItem> getCommdoityPropertyItemsWithoutPackage() {
        if (commdoityPropertyItemsWithoutPackage == null) {
            commdoityPropertyItemsWithoutPackage = new ArrayList();
            for (CommodityPropertyEnum cce : CommodityPropertyEnum.values()) {
                if (!cce.equals(cce.套系商品)) {
                    commdoityPropertyItemsWithoutPackage.add(new SelectItem(cce, cce.toString()));
                }
            }
        }
        return commdoityPropertyItemsWithoutPackage;
    }

    public List<SelectItem> getMaterialTypeSelectItems() {
        List commodityTypeItems = getCommodityTypeItemMap().get(CommodityPropertyEnum.材料商品);
        return commodityTypeItems;
    }

    /**
     * @param commdoityPropertyItemsWithoutPackage the commdoityPropertyItemsWithoutPackage to set
     */
    public void setCommdoityPropertyItemsWithoutPackage(List<SelectItem> commdoityPropertyItemsWithoutPackage) {
        this.commdoityPropertyItemsWithoutPackage = commdoityPropertyItemsWithoutPackage;
    }

    /**
     * @return the employeeSelectItems
     */
    public List<SelectItem> getEmployeeSelectItems() {
        if (employeeSelectItems == null) {
            List<Employee> employees = employeeFacade.findAllActive();
            employeeSelectItems = new ArrayList();
            employeeSelectItems.add(new SelectItem(null, "请选择员工"));
            for (Employee e : employees) {
                employeeSelectItems.add(new SelectItem(e, e.toString()));
            }
        }
        return employeeSelectItems;
    }

    /**
     * @param employeeSelectItems the employeeSelectItems to set
     */
    public void setEmployeeSelectItems(List<SelectItem> employeeSelectItems) {
        this.employeeSelectItems = employeeSelectItems;
    }

    /**
     * @return the departmentPostSelectItems
     */
    /**
     * @return the departmentPostSelectItems
     */
    public List<SelectItem> getDepartmentPostSelectItems() {
        if (departmentPostSelectItems == null) {
            departmentPostSelectItems = new ArrayList();
            departmentPostSelectItems.add(new SelectItem(null, "请选择职位"));
            List<DepartmentPost> departmentPosts = departmentPostFacade.findAll();
            for (DepartmentPost dp : departmentPosts) {
                departmentPostSelectItems.add(new SelectItem(dp, dp.toString()));
            }
        }
        return departmentPostSelectItems;
    }

    /**
     * @param departmentPostSelectItems the departmentPostSelectItems to set
     */
    public void setDepartmentPostSelectItems(List<SelectItem> departmentPostSelectItems) {
        this.departmentPostSelectItems = departmentPostSelectItems;
    }

    /**
     * @return the employSelectItemMap
     */
    public Map<Integer, List<SelectItem>> getEmployeeSelectItemMap() {
        if (employeeSelectItemMap == null) {
            employeeSelectItemMap = new HashMap();
            employeeSelectItemMap.put(0, getEmployeeSelectItems());
            for (DepartmentPost dp : departmentPostFacade.findAll()) {
                List emSelectItems = new ArrayList();
                for (Employee e : employeeFacade.searchByDepartment(dp)) {
                    emSelectItems.add(new SelectItem(e, e.toString()));
                }
                employeeSelectItemMap.put(dp.getId(), emSelectItems);
            }
        }
        return employeeSelectItemMap;
    }

    /**
     * @param employSelectItemMap the employSelectItemMap to set
     */
    public void setEmployeeSelectItemMap(Map<Integer, List<SelectItem>> employeeSelectItemMap) {
        this.employeeSelectItemMap = employeeSelectItemMap;
    }

    /**
     * @return the formTypeSelectList
     */
    public List<SelectItem> getFormTypeSelectList() {
        if (formTypeSelectList == null) {
            formTypeSelectList = new ArrayList();
            formTypeSelectList.add(new SelectItem(null, "请选择"));
            for (FormTypeEnum fte : FormTypeEnum.values()) {
                formTypeSelectList.add(new SelectItem(fte, fte.toString()));
            }
        }
        return formTypeSelectList;
    }

    /**
     * @param formTypeSelectList the formTypeSelectList to set
     */
    public void setFormTypeSelectList(List<SelectItem> formTypeSelectList) {
        this.formTypeSelectList = formTypeSelectList;
    }

    /**
     * @return the paymentMethodSelectItems
     */
    public List<SelectItem> getPaymentMethodSelectItems() {
        return paymentMethodSelectItems;
    }

    /**
     * @param paymentMethodSelectItems the paymentMethodSelectItems to set
     */
    public void setPaymentMethodSelectItems(List<SelectItem> paymentMethodSelectItems) {
        this.paymentMethodSelectItems = paymentMethodSelectItems;
    }

    /**
     * @return the postingTypeSelectItems
     */
    public List<SelectItem> getPostingTypeSelectItems() {
        if (postingTypeSelectItems == null) {
            postingTypeSelectItems = new ArrayList();
            postingTypeSelectItems.add(new SelectItem("", "请选择"));
            for (PostingTypeEnum pte : PostingTypeEnum.values()) {
                postingTypeSelectItems.add(new SelectItem(pte.toString(), pte.toString()));
            }
        }
        return postingTypeSelectItems;
    }

    /**
     * @param postingTypeSelectItems the postingTypeSelectItems to set
     */
    public void setPostingTypeSelectItems(List<SelectItem> postingTypeSelectItems) {
        this.postingTypeSelectItems = postingTypeSelectItems;
    }

    public TimeZone getTimeZone() {
        return TimeZone.getDefault();
    }

    /**
     * @return the royaltyCalculatorSelectItems
     */
    public List<SelectItem> getPayrollCalculatorSelectItems() {
        if (payrollCalculatorSelectItems == null) {
            payrollCalculatorSelectItems = new ArrayList();
            payrollCalculatorSelectItems.add(new SelectItem(null, "请选择"));
            for (PayrollCalculatorEnum pce : PayrollCalculatorEnum.values()) {
                payrollCalculatorSelectItems.add(new SelectItem(pce, pce.toString()));
            }
        }
        return payrollCalculatorSelectItems;
    }

    /**
     * @param royaltyCalculatorSelectItems the royaltyCalculatorSelectItems to set
     */
    public void setPayrollCalculatorSelectItems(List<SelectItem> payrollCalculatorSelectItems) {
        this.payrollCalculatorSelectItems = payrollCalculatorSelectItems;
    }

    public List<SelectItem> getRetailMaterialCommodityPropertyItems() {
        List<SelectItem> selectItems = new ArrayList<SelectItem>();
        selectItems.add(new SelectItem(null, "请选择"));
        selectItems.add(new SelectItem(CommodityPropertyEnum.材料商品, CommodityPropertyEnum.材料商品.toString()));
        selectItems.add(new SelectItem(CommodityPropertyEnum.零售商品, CommodityPropertyEnum.零售商品.toString()));
        return selectItems;
    }
}
